package com.lyh.societiesmanager.controller;

import com.lyh.societiesmanager.ClubService.MemberService;
import com.lyh.societiesmanager.entity.Member;
import com.lyh.societiesmanager.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/members")
@Api(tags = "成员管理接口")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation("查询所有成员")
    @GetMapping
    public Response<List<Member>> getAllMembers() {
        return Response.success(memberService.list());
    }

    @ApiOperation("根据ID查询成员")
    @GetMapping("/{id}")
    public Response<Member> getMemberById(@PathVariable Integer id) {
        Member member = memberService.getById(id);
        return member != null ? Response.success(member) : Response.error(404, "未找到该成员");
    }

    @ApiOperation("新增成员")
    @PostMapping
    public Response<Boolean> addMember(@RequestBody Member member) {
        return memberService.save(member) ? Response.success("添加成功", true) : Response.error(500, "添加失败");
    }

    @ApiOperation("更新成员信息")
    @PutMapping("/{id}")
    public Response<Boolean> updateMember(@PathVariable Integer id, @RequestBody Member member) {
        member.setMemberId(id);
        return memberService.updateById(member) ? Response.success("更新成功", true) : Response.error(500, "更新失败");
    }

    @ApiOperation("删除成员")
    @DeleteMapping("/{id}")
    public Response<Boolean> deleteMember(@PathVariable Integer id) {
        return memberService.removeById(id) ? Response.success("删除成功", true) : Response.error(500, "删除失败");
    }
}